package mju.cxf.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;


import mju.cxf.dto.ProductDTO;

import mju.cxf.entity.Product;
import mju.cxf.result.Result;
import mju.cxf.service.ProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品相关接口
 */

@RestController
@Api(tags = "商品接口列表")
@RequestMapping("/admin/api/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 新增商品信息
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新建商品信息")
    public Result<String> createProduct(@RequestBody ProductDTO productDTO) {
        return productService.saveProduct(productDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }



    /**
     * 修改商品信息
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品信息")
    public Result<String> updateProduct(@RequestBody ProductDTO productDTO) {
        return productService.updateProduct(productDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 删除商品信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品信息", notes = "管理员通过此接口删除商品信息")
    public Result<String> deleteProduct(@PathVariable Integer id) {
        return productService.deleteProduct(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }
    /**
     * U分页查询商品信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    public Page<ProductDTO> getUsersByUsername(@RequestParam(value = "name",required = false) String name,
                                               @RequestParam(value = "state",required = false) Integer state,
                                               @RequestParam(value = "current",defaultValue = "1") int current,
                                               @RequestParam(value = "size",defaultValue = "10") int size) {
        return productService.selectPageByName(name, state, current, size);
    }


}
